import apiRequest from './request'
import type { ApiResponse, User } from '@/types'

// 获取用户列表
export const getUserListApi = (params?: any): Promise<ApiResponse<User[]>> => {
  return apiRequest.get('/user/list', { params }).then(res => Array.isArray(res) ? res : (res.data || res))
}

// 创建用户
export const createUserApi = (data: User): Promise<ApiResponse<User>> => {
  return apiRequest.post('/user/create', data).then(res => res.data || res)
}

// 更新用户
export const updateUserApi = (data: User): Promise<ApiResponse<User>> => {
  return apiRequest.post('/user/update', data).then(res => res.data || res)
}

// 删除用户
export const deleteUserApi = (id: number): Promise<ApiResponse<string>> => {
  return apiRequest.post(`/user/delete/${id}`).then(res => res.data || res)
}

// 获取用户角色
export const getUserRolesApi = (userId: number): Promise<ApiResponse<any>> => {
  return apiRequest.get(`/user/${userId}/roles`).then(res => Array.isArray(res) ? res : (res.data || res))
}

// 分配用户角色
export const assignUserRolesApi = (userId: number, roleIds: number[]): Promise<ApiResponse<any>> => {
  return apiRequest.post(`/user/${userId}/roles`, roleIds).then(res => res.data || res)
}